﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public static class IEnumeratorExtentions  {

    public static T GetMin<T>(this IEnumerable<T> list, Func<T, FixedFloat> getValue) where T:class
	{
		if (list.Count<T>() == 0)
			return null;

		T min = default(T);
        FixedFloat curMinValue = int.MaxValue;
		foreach (var element in list)
		{
            if (getValue(element) < curMinValue)
			{
                curMinValue = getValue(element);
				min = element;
			}
		}
        return min;
	}
	
	public static T GetMinValue<T>(this IEnumerable<T> list, Func<T, FixedFloat> getValue) 
	{
		if (list.Count<T>() == 0)
			return default(T);

		T min = default(T);
        FixedFloat curMinValue = int.MaxValue;
		foreach (var element in list)
		{
			if (getValue(element) < curMinValue)
			{
				curMinValue = getValue(element);
				min = element;
			}
		}
		return min;
	}  
	
	//public static T GetMax<T>(this IEnumerable<T> list, Func<T, FixedFloat> getValue) where T : class
	//{
	//	if (list.Count<T>() == 0)
	//		return null;

	//	T max = default(T);
	//	float curMaxValue = float.NegativeInfinity;
	//	foreach (var element in list)
	//	{
	//		if (getValue(element) > curMaxValue)
	//		{
	//			curMaxValue = getValue(element);
	//			max = element;
	//		}
	//	}
	//	return max;
	//}  
	
	public static T GetMaxValue<T>(this IEnumerable<T> list, Func<T, float> getValue) 
	{
		if (list.Count<T>() == 0)
			return default(T);

		T max = default(T);
		float curMaxValue = float.NegativeInfinity;
		foreach (var element in list)
		{
			if (getValue(element) > curMaxValue)
			{
				curMaxValue = getValue(element);
				max = element;
			}
		}
		return max;
	}
	
}
